package com.mes.eventhandler;

import com.mes.data.EmesCalendarTemplateTime;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.SessionInfo;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;
import java.util.Calendar;
import java.util.Date;

public class CalendarTemplateEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = new Entity[]{ModelProvider.getInstance().getEntity(EmesCalendarTemplateTime.ENTITY_NAME)};

	// protected Logger logger = Logger.getLogger(this.getClass());
	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesCalendarTemplateTime emesCalendarTemplateTime = (EmesCalendarTemplateTime) event.getTargetInstance();
		// 开始时间
		final Date startTime = emesCalendarTemplateTime.getTimeFrom();
		Calendar calendar_start = Calendar.getInstance();
		calendar_start.setTime(startTime);
		int startH = calendar_start.get(Calendar.HOUR_OF_DAY);
		// 结束时间
		final Date endTime = emesCalendarTemplateTime.getTimeTo();
		Calendar calendar_end = Calendar.getInstance();
		calendar_end.setTime(endTime);
		int endH = calendar_end.get(Calendar.HOUR_OF_DAY);
		// 验证时间的正确性
		if (endH < startH) {
			String language = OBContext.getOBContext().getLanguage().getLanguage();
			ConnectionProvider conn = new DalConnectionProvider(false);
			throw new OBException(Utility.messageBD(conn, "您输入的时间有误。请仔细检查。", language));
		}
	}
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesCalendarTemplateTime emesCalendarTemplateTime = (EmesCalendarTemplateTime) event.getTargetInstance();
		// 开始时间
		final Date startTime = emesCalendarTemplateTime.getTimeFrom();
		Calendar calendar_start = Calendar.getInstance();
		calendar_start.setTime(startTime);
		int startH = calendar_start.get(Calendar.HOUR_OF_DAY);
		// 结束时间
		final Date endTime = emesCalendarTemplateTime.getTimeTo();
		Calendar calendar_end = Calendar.getInstance();
		calendar_end.setTime(endTime);
		int endH = calendar_end.get(Calendar.HOUR_OF_DAY);
		// 验证时间的正确性
		if (endH < startH) {
			String language = OBContext.getOBContext().getLanguage().getLanguage();
			ConnectionProvider conn = new DalConnectionProvider(false);
			throw new OBException(Utility.messageBD(conn, "您输入的时间有误。请仔细检查。", language));
		} else {
			// 获取tabId，用来判断星期几
			String tabId = SessionInfo.getProcessId();
			if ("006A048B0DDF42CF932243C44B903F27".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "1");
			} else if ("F1CFD2F6C5C544A392836FF151C3CFD9".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "2");
			} else if ("CC515860EF3643238494DBD681BD0C04".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "3");
			} else if ("771409D742A24C57AF8BD8DF1AC58F5B".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "4");
			} else if ("3317509B177A4C28AC53D65771CCBB90".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "5");
			} else if ("A222043E729B46C9AEE6FC46BE4010BF".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "6");
			} else if ("CC6DE93AB84B4F33A7D8E32BEE49B39C".equalsIgnoreCase(tabId)) {
				final Property WeekdayProperty = entities[0].getProperty(EmesCalendarTemplateTime.PROPERTY_WEEKDAY);
				event.setCurrentState(WeekdayProperty, "0");// 零代表星期日
			}
		}
	}
	public void onDelete(@Observes EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
}
